package com.logic.landseaserver.service.callback;

import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.domain.RealBill;
import com.logic.landseaserver.models.independent.service.BillIndependentService;
import com.logic.landseaserver.persistence.read.RealBillReadMapper;
import com.logic.landseaserver.persistence.write.RealBillWriteMapper;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：RealBillBusinessService</br>
 * 类描述：实时账单回调业务逻辑</br>
 * 创建人：Tony</br>
 * 创建时间：2017年7月5日 下午4:49:30</br>
 * 
 * @version 1.0
 *
 */
@Service("WebankCallBack_BT3")
public class RealBillBusinessService implements IWebankCallbackHandler
{
    private static final Logger LOG = LoggerFactory.getLogger(RealBillBusinessService.class);
    
    @Autowired
    private RealBillWriteMapper realBillWriteMapper;
    
    @Autowired
    private RealBillReadMapper realBillReadMapper;
    
    @Autowired
    private BillIndependentService billIndependentService;
    
    @Override
    public Object process(HashMap<String, Object> param)
        throws LandseaException
    {
        LOG.info("RealBillBusinessService|实时账单业务处理start");
        Integer billId = (Integer)param.get("billId");
        Date payTime = (Date)param.get("payTime");
        try
        {
            // 实时账单
            RealBill realBill = realBillReadMapper.selectByPrimaryKey(billId);
            if (null == realBill)
            {
                LOG.error("RealBillBusinessService|process|no record in ls_real_bill table with billId=" + billId);
                throw new LandseaBusinessException(LandseaErrorMessageEnum.DEPOSIT_BILL_NOT_EXISTS_NULL);
            }
            
            realBillWriteMapper.updateStatById(realBill);
            //pos和微众支付修改balanceSheet表中做修改
            billIndependentService.realPayBalanceSheet(billId, payTime);
        }
        catch (Exception e)
        {
            LOG.error("WebankCallBack_BT3 |handle callback event failed.", e);
            throw LandseaException.createException(LandseaErrorMessageEnum.WEBANK_CALLBACK_BT3_PARAM_ERROR, e);
        }
        LOG.info("实时账单业务处理end");
        return null;
    }
    
}
